package geowiki;

import geowiki.data.Place;
import geowiki.data.Link;
import geowiki.data.PlacemakerPlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLQuery;
import geowiki.utils.WikipediaUtils;
import geowiki.vis.LineFromPlaces;

import java.util.ArrayList;
import java.util.List;


import processing.core.PApplet;

public class GeoLinkApp extends GeoWikiApp {

    List<Link> links;
    LineFromPlaces lines;

    ;

    public void setup() {
        super.setup();

        lines = new LineFromPlaces(this);

        // String article = "Frank_Brettschneider";
        // String article = "Sasha_Grey";
        // String article = "Oskar_Schlemmer";
        String article = "Tango";


        List<Place> places = loadPlacesViaPlacemakerFromWiki(article);


        placeMarkers = createPlaceMarkers(places);

        //** this works but will take ages to complete!!

        links = loadLinks(article);
        List<List<Place>> linkedplaces = loadLinkedPlaces(links, article);

        for (int i = 0; i < links.size(); i++) {

            if (linkedplaces.size() != 0) {
                links.get(i).storePlaceMarkers(linkedplaces.get(i));
            }
            println("for link " + i + " storing " + linkedplaces.size() + " places");
        }

    }

    public void draw() {
        background(0);

        strokeWeight(1);
        for (int i = 0; i < links.size(); i++) {
            println("drawing " + i + "  at X");
            lines.drawLines(links.get(i).storedPlaceMarkers, color(0, 0, 255), 25,1);
            
            
            int color = color(255,200,4);
            for (int j=0;j<links.get(i).storedPlaceMarkers.size();j++){
            links.get(i).storedPlaceMarkers.get(i).displaySmall(color);
            }

        }

        strokeWeight(2);
        lines.drawLines(placeMarkers, color(255, 0, 0), 100,1);


    }

    ///** Will extract the LinkedPlaceloader soon into extra class for threading
    public List<List<Place>> loadLinkedPlaces(List<Link> links, String wikipediaArticleName) {

        links = loadLinks(wikipediaArticleName);
        List<List<Place>> linkedPlaces = new ArrayList<List<Place>>();

        for (int i = 0; i < links.size(); i++) {

            println(links.get(i) + " /" + i + " of " + links.size());
            String linkURL = WikipediaUtils.getFullURLFromLink(links.get(i).url);
            println("fullURL:" + linkURL);
            List<Place> placesOfLink = loadPlacesViaPlacemakerFromURL(linkURL);


            println("PlacesFound:" + placesOfLink);
            linkedPlaces.add(i, placesOfLink);

            //links.get(i).storePlaceMarkers(placesOfLink);
        }

        return linkedPlaces;
    }

    public List<Link> loadLinks(String wikipediaArticleName) {

        YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
        String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
        String restURL = YQLQuery.getAllLinksFromWebpage(wikipediaURL);
        List<Link> alllinks = linkExtractor.getLinks(restURL);
        return alllinks;

    }

    public List<Place> loadPlacesViaPlacemakerFromWiki(String wikipediaArticleName) {

        String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
        List<Place> allplaces = loadPlacesViaPlacemakerFromURL(wikipediaURL);

        return allplaces;
    }

    public List<Place> loadPlacesViaPlacemakerFromURL(String URL) {


        List<Place> allPlaces = new ArrayList<Place>();

        YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
        String[] paragraphs = linkExtractor.getMainParagraphs(URL);

        PlacemakerPlaceExtractor placeExtractor = new PlacemakerPlaceExtractor(this);
        for (int i = 0; i < paragraphs.length; i++) {
            PApplet.println("Getting places for " + i + ". p");
            List<Place> places = placeExtractor.getPlaces(paragraphs[i]);

            PApplet.println("\tplaces=" + places);
            allPlaces.addAll(places);
        }
        return allPlaces;
    }
}

